import { Component, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { environment } from '../../../environments/environment';
import { RiskBaseComponent } from '../../shared/components/riskBase.component';
//embedded components
import { RatingOverviewMeiComponent } from './components/ratingOverview.component';
import { AnalystViewMeiComponent } from './components/analystView.component';
import { SubIndustryMeiComponent } from './components/subIndustry.component';
import { MeiIndicatorsComponent } from './components/indicators.component';
//Services
import { CompanyRiskService } from '../companyRisk.service';
import { AnalyticsService } from '../../shared/services/analytics.service'

//Models
import { GoogleAnalyticsBeacon } from '../../models/googleAnalytics.beacon'

@Component({
  templateUrl: './mei.component.html'
})
export class RiskMeiComponent extends RiskBaseComponent {  
  private sub: any;
  private externalId: string;
  public type: string;
  public mei: any;
  public subPeerGroup: string;
  public readonly resourcesUrl: string = environment.resourcesUrl;

  private promises: Promise<void>[];

  @ViewChild('ratingOverview') ratingOverview: RatingOverviewMeiComponent;    
  @ViewChild('analystView') analystView: AnalystViewMeiComponent;
  @ViewChild('subIndustry') subIndustry: SubIndustryMeiComponent;
  @ViewChild('indicators') indicators: MeiIndicatorsComponent;
  
  constructor(
    private route: ActivatedRoute,
    private riskService: CompanyRiskService,
    private analyticsService: AnalyticsService,
    private http: Http) {
    super(http);
  }

  ngOnInit() {    
    this.sub = this.route.params.subscribe(params => {  
      this.gaService.showPreloader();
      this.externalId = params['externalId'];
      this.type = params['type'];
      this.getMei();
    });  
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }

  ngAfterViewInit(){    
    this.promises = [this.ratingOverview.loaded, this.indicators.loaded];
    if(this.type === 'MaterialIssue')
      this.promises.push(this.analystView.loaded);    

    if(this.type !== 'IdiosyncraticIssue')
      this.promises.push(this.subIndustry.loaded);
  }

  private getMei(): void {
    this.riskService.company.then((company) => {
      this.subPeerGroup = company.subPeerGroup;
      this.riskService.mei = this.riskService.getMeiById(company.id, this.externalId);
      this.riskService.mei.then((data) => {        
        this.mei = data;        

        Promise.all(this.promises).then(() => {      
          this.gaService.hidePreloader();
        });

        if (data.externalId == 0)
          this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('ESG RR CG', 'ESG Risk Rating', { CompanyId: company.id }));
        else
          this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('ESG RR MEIs', 'ESG Risk Rating', { CompanyId: company.id, MEIName: data.name }));
          
      })
      .catch((msg) => console.log(msg));;
    });
  }
}